﻿using Core.SimpleTemp.Application.RoleApp;
using Core.SimpleTemp.Application.SysApp.RoleApp.Dtos;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api.Sys
{
    /// <summary>
    /// 角色
    /// </summary>
    [ApiExplorerSettings(GroupName = "Sys")]
    [Route("api/[controller]")]
    [EnableCors("any")]
    [ApiController]
    [Authorize]
    public class RoleController : CoreApiController
    {
        private readonly SysRoleAppService _service;
        private readonly ILogger<RoleController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysRoleAppService"></param>
        /// <param name="logger"></param>
        public RoleController(SysRoleAppService sysRoleAppService, ILogger<RoleController> logger)
        {
            _service = sysRoleAppService;
            _logger = logger;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        [PermissionFilter(nameof(RoleController) + "_" + nameof(RoleController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<SysRoleDto> pagingQueryModel)
        {
            var result = await _service.GetListAsync(pagingQueryModel);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 增/改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Save")]
        [PermissionFilter(nameof(RoleController) + "_" + nameof(RoleController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult> SaveAsync(SysRoleDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(RoleController) + "_" + nameof(RoleController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {
            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }

        /// <summary>
        /// 根据ID获取信息
        /// </summary>
        /// <param name="id">Guid</param>
        /// <returns></returns>
        [HttpGet("Get")]
        [PermissionFilter(nameof(RoleController) + "_" + nameof(RoleController.Get), AuthorizationConst.GET)]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            return this.JsonSuccess(model);
        }
        #endregion

        /// <summary>
        /// 获取系统角色信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetRoleList/{factoryId?}")]
        public async Task<IActionResult> GetRoleListAsync([FromRoute] Guid? factoryId)
        {
            var list = await this._service.GetRoleListAsync(factoryId);
            return this.JsonSuccess(list);
        }



        /// <summary>
        /// 根据角色获取权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("GetMenuListByRole")]
        public async Task<IActionResult> GetMenuListByRole(Guid roleId)
        {
            var list = await _service.GetMenuListByRoleAsync(roleId);
            return this.JsonSuccess(list);
        }

        /// <summary>
        /// 更新角色权限关联关系
        /// </summary>
        /// <param name="updateRoleMenuInputModel"></param>
        /// <returns></returns>
        [HttpPost("UpdateRoleMenu")]
        [PermissionFilter(nameof(RoleController) + "_" + nameof(RoleController.UpdateRoleMenu), "更新角色权限关联关系【功能管理】")]
        public async Task<IActionResult> UpdateRoleMenu(UpdateRoleMenuInputModel updateRoleMenuInputModel)
        {
            List<SysRoleMenuDto> list = new List<SysRoleMenuDto>();
            foreach (var item in updateRoleMenuInputModel.MenuIds)
            {
                list.Add(new SysRoleMenuDto
                {
                    SysMenuId = item,
                    SysRoleId = updateRoleMenuInputModel.RoleId
                });
            }
            await _service.UpdateRoleMenuAsync(updateRoleMenuInputModel.RoleId, list);
            return this.JsonSuccess();
        }
    }
}